package com.ikas.ai.server.module.data.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.ikas.ai.handle.JsonResult;
import com.ikas.ai.server.aop.Pageable;
import com.ikas.ai.server.module.AbstractBean;
import com.ikas.ai.server.module.data.enums.SignalType;
import com.ikas.ai.server.module.data.model.dto.TripMeteAddDTO;
import com.ikas.ai.server.module.data.model.dto.TripMeteExportDTO;
import com.ikas.ai.server.module.data.model.dto.TripMeteUpdateDTO;
import com.ikas.ai.server.module.data.service.TripMeteService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 跳机测点管理
 *
 * @Author: qi.yilai
 * @Date: 2023/2/2 17:47
 * @Version: 1.0.0
 **/
@RestController
@RequestMapping("/trip/mete")
public class TripMeteController extends AbstractBean {

    @Autowired
    private TripMeteService meteService;

    /**
     * 新增测点
     *
     * @param meteAddDTO
     * @return
     */
    @PostMapping("/add")
    public JsonResult add(@Validated @RequestBody TripMeteAddDTO meteAddDTO) {
        return JsonResult.ok(meteService.add(meteAddDTO, getUserId()));
    }

    /**
     * 更新测点
     *
     * @param meteUpdateDTO
     * @return
     */
    @PostMapping("/update")
    public JsonResult update(@Validated @RequestBody TripMeteUpdateDTO meteUpdateDTO) {
        return JsonResult.ok(meteService.update(meteUpdateDTO, getUserId()));
    }

    /**
     * 根据设备编号获取测点
     *
     * @param equipCode
     * @return
     */
    @GetMapping("/getMetesByEquipmentCode/{equipCode}")
    public JsonResult getMetesByDeviceId(@PathVariable("equipCode") String equipCode) {
        return JsonResult.ok(meteService.getMetesByEquipmentCode(equipCode));
    }

    /**
     * 分页获取测点
     *
     * @return
     */
    @Pageable(sortType = "desc", sortBy = "createTime")
    @GetMapping("/listMetesByPage")
    public JsonResult listMetesByPage(@RequestParam(required = false) String meteCode,
                                      @RequestParam(required = false) String tripType,
                                      @RequestParam(required = false) String meteId,
                                      @RequestParam(required = false) String signalCnName,
                                      @RequestParam(required = false) String machineNo,
                                      @RequestParam(value = "oid", required = false) Long oid,
                                      @RequestParam(value = "parentNodeName", required = false) String parentNodeName,
                                      @RequestParam(value = "nodeName", required = false) String nodeName,
                                      @RequestParam(required = false) String equipmentCode
    ) {
        return JsonResult.ok(meteService.queryMetes(meteCode, tripType, meteId, signalCnName, machineNo, oid, parentNodeName, nodeName, equipmentCode));
    }

    /**
     * 删除测点
     *
     * @param id
     * @return
     */
    @PostMapping("deleteMete/{id}")
    public JsonResult deleteMete(@PathVariable("id") Long id) {
        meteService.delete(id);
        return JsonResult.ok();
    }

    /**
     * 批量删除测点
     *
     * @param ids
     * @return
     */
    @PostMapping("batchDeleteMete")
    public JsonResult deleteMete(@RequestParam("ids") List<Long> ids) {
        meteService.deleteByIds(ids);
        return JsonResult.ok();
    }

    /**
     * 批量导入测点
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/import")
    public JsonResult importMetes(@RequestParam MultipartFile file) throws Exception {
        meteService.importMetes(file, getUserId());
        return JsonResult.ok();
    }

    /**
     * 测点类型列表
     *
     * @return
     */
    @GetMapping("/signalType/list")
    public JsonResult signalTypeList() {
        return JsonResult.ok(SignalType.getTypeList());
    }

    /**
     * 导入跳机测点
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/importTripMete")
    public JsonResult importTripMete(@RequestParam MultipartFile file) throws Exception {
        meteService.importTripMete(file, getUserId());
        return JsonResult.ok();
    }

    /**
     * 导出跳机测点
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/exportTripMete")
    public JsonResult exportTripMete(HttpServletResponse response,
                                     @RequestParam(required = false) String meteCode,
                                     @RequestParam(required = false) String tripType,
                                     @RequestParam(required = false) Long meteId,
                                     @RequestParam(required = false) String signalCnName,
                                     @RequestParam(required = false) String machineNo,
                                     @RequestParam(value = "oid", required = false) Long oid,
                                     @RequestParam(value = "parentNodeName", required = false) String parentNodeName,
                                     @RequestParam(value = "nodeName", required = false) String nodeName,
                                     @RequestParam(required = false) String equipmentCode) throws Exception {
        Workbook workbook = meteService.exportTripMete(meteCode, tripType, meteId, signalCnName, machineNo, oid, parentNodeName, nodeName, equipmentCode, getUserId());
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + URLEncoder.encode("跳机测点.xlsx", "UTF-8"));
        response.setCharacterEncoding("UTF-8");
        workbook.write(response.getOutputStream());
        return JsonResult.ok();
    }

    /**
     * 跳机测点导入模板
     *
     * @param response
     * @return
     * @throws Exception
     */
    @PostMapping("/template")
    public JsonResult downloadTemplate(HttpServletResponse response) throws Exception {
        String sheetName = "跳机测点";
        String filename = "跳机测点导入模板.xlsx";
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(null, sheetName, ExcelType.XSSF), TripMeteExportDTO.class, new ArrayList<>());
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + URLEncoder.encode(filename, "UTF-8"));
        response.setCharacterEncoding("UTF-8");
        workbook.write(response.getOutputStream());
        return JsonResult.ok();
    }
}
